package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.*;
import com.newsystem.pojo.*;
import com.newsystem.pojo.vo.MediumInfo;
import com.newsystem.service.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
public class NewsService extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MediumMapper mediumMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private TfileMapper tfileMapper;
    @Autowired
    private NewsVideoMapper newsVideoMapper;
    @Autowired
    private NewsPhotoMapper newsPhotoMapper;
    @Autowired
    private NewsCoverMapper newsCoverMapper;

    //发布新闻视频
    public CommonResult<Object> insertVideoNews(String userId, String content, String videoUrl,String title,String coverUrl) {
        //根据userId获取媒体id
        MediumInfo mediumInfo = userMapper.selectMedium(userId);
        String mediumId = mediumInfo.getMedium().getId();
        if(mediumId==null){
            return CommonResult.failed("媒体用户失败");
        }
        News news = new News();
        Tfile tfile = new Tfile();
        NewsCover cover = new NewsCover();
        NewsVideo newsVideo = new NewsVideo();
        news.setId(MyUUIDUtils.getUUID());
        news.setCollectionNum(0);
        news.setGoodNum(0);
        news.setContent(content);
        news.setMediumId(mediumId);
        tfile.setId(MyUUIDUtils.getUUID());
        tfile.setNewsId(news.getId());
        cover.setId(MyUUIDUtils.getUUID());
        cover.setNewsId(news.getId());
        cover.setCoverUrl(coverUrl);
        cover.setTitle(title);
        newsVideo.setId(MyUUIDUtils.getUUID());
        newsVideo.setFileId(tfile.getId());
        newsVideo.setVideoUrl(videoUrl);
        if (newsMapper.insert(news) > 0){
            if (tfileMapper.insert(tfile) > 0 && newsCoverMapper.insert(cover) > 0){
                if(newsVideoMapper.insert(newsVideo) > 0){
                    Map<String ,Object> map = new HashMap<>();
                    map.put("newsId",news.getId());
                    return CommonResult.success(map);
                }
            }
        }
        return CommonResult.failed("发布失败");
    }

    //根据新闻id获得新闻内容
    public CommonResult<Object> selectNewsInfo(String newsId) {
        News news = newsMapper.selectById(newsId);
        if(news==null){
            return CommonResult.failed("查无此新闻");
        }
        List<String> newsVideoUrlList = null;
        List<String> newPhotoUrlList = null;
        Tfile tfile = tfileMapper.selectByNewsId(newsId);
        if(tfile != null){
            newsVideoUrlList = newsVideoMapper.selectByFileId(tfile.getId());
            newPhotoUrlList = newsPhotoMapper.selectByFileId(tfile.getId());
        }
        Map<String ,Object> map = new HashMap<>();
        map.put("id",news.getId());
        map.put("mediumId", news.getMediumId());
        map.put("content",news.getContent());
        map.put("collentionNum",news.getCollectionNum());
        map.put("goodNum",news.getGoodNum());
        map.put("voideoList",newsVideoUrlList);
        map.put("photoList",newPhotoUrlList);
        map.put("createtime",news.getCreatetime());
        map.put("updatetime",news.getUpdatetime());
        return CommonResult.success(map);
    }

    //修改文本新闻内容
    public CommonResult<Object> updateTextNew(String userId, String newsId, String content) {
        //根据userId获取媒体id
        MediumInfo mediumInfo = userMapper.selectMedium(userId);
        String mediumId = mediumInfo.getMedium().getId();
        if(mediumId == null){
            return CommonResult.failed("媒体用户失败");
        }
        User user = userMapper.selectById(userId);
        if(user != null && user.getStatue() == 1){
            News news = newsMapper.selectByMediumIdNewId(newsId,mediumId);
            if(news==null){
                return CommonResult.failed("此新闻不存在");
            }
            news.setContent(content);
            if(newsMapper.updateById(news) > 0){
                return CommonResult.success("更新内容成功");
            }
        }
        return CommonResult.failed("此用户不存在");

    }

    //发布文本新闻
    public CommonResult<Object> insertTestNews(String userId, String content, String title, String coverUrl) {
        //根据userId获取媒体id
        MediumInfo mediumInfo = userMapper.selectMedium(userId);
        String mediumId = mediumInfo.getMedium().getId();
        if(mediumId==null){
            return CommonResult.failed("媒体用户失败");
        }
        News news = new News();
        NewsCover cover = new NewsCover();
        news.setId(MyUUIDUtils.getUUID());
        news.setMediumId(mediumId);
        news.setContent(content);
        news.setGoodNum(0);
        news.setCollectionNum(0);
        cover.setId(MyUUIDUtils.getUUID());
        cover.setNewsId(news.getId());
        cover.setCoverUrl(coverUrl);
        cover.setTitle(title);
        if(newsMapper.insert(news) > 0 && newsCoverMapper.insert(cover) > 0){
            Map<String ,Object> map = new HashMap<>();
            map.put("newsId",news.getId());
            return CommonResult.success(map);
        }
        return CommonResult.failed("发布文本新闻失败");
    }

    //todo 批量删除新闻
    public CommonResult<Object> deleteNews(String userId,List<String> newsId) {
        MediumInfo mediumInfo = userMapper.selectMedium(userId);
        String mediumId = mediumInfo.getMedium().getId();
        if(mediumId==null){
            return CommonResult.failed("媒体用户失败");
        }
        tfileMapper.deleteFile(newsId);
        if(newsMapper.deleteNews(mediumId,newsId) > 0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }
}
